// miniprogram/pages/bar/index/managers/belas/belas.js
const APIUrl = require('../../../../../tools/APIurl');
const mAPIUrl = new APIUrl()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTitle: [{
      title: "已到账",
      titleId: "1",
      creatPoint: "set_aside",
      accounts:0

    }, {
      title: "应收账",
      titleId: "2",
      creatPoint: "used",
      accounts:3
    }],
    bill:[{
      rent:'1880.00',
      tenants:'僵尸',
      roomNumber:'606',
      assertTrue:'2021-02-22',
      show:false,
      remindRent:0,
    },{
      rent:'1600.00',
      tenants:'猪头',
      roomNumber:'607',
      assertTrue:'2021-02-22',
      show:true,
      remindRent:5,
    },{
      rent:'230.00',
      tenants:'史莱姆',
      roomNumber:'202',
      assertTrue:'2021-02-22',
      show:false,
      remindRent:0,
    },
  ],
    chartSelect: 1,
    idx: "1",
    total:0,
    index:0
  },
  selectApply: function (e) {
    var that = this;
    var id = e.target.dataset.id
    var list = that.data.topTitle
    that.setData({
        idx: id,
        chartSelect: id,
        currentPoint: list[id - 1].creatPoint
      }),
      console.log(that.data.currentPoint)
  },
  rentBtn(e){
    console.log(e)
    var id = that.data.guestslist[e.currentTarget.dataset.id].id
    var arrayIndex=that.data.arrayIndex[that.data.index]
    var index = that.data.array[that.data.index]
    var number = e.currentTarget.dataset.number
    var start_rent = that.data.guestslist[e.currentTarget.dataset.id].start_rent
    wx.navigateTo({
      url: '/pages/bar/index/managers/lease/changeRentDate/changeRentDate?address_id='+arrayIndex+'&address='+index+'&number='+number+'&id='+id+'&start_rent='+start_rent,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    var year=new Date().getFullYear()
    var month=new Date().getMonth()+1
    var day = new Date().getDate()
    console.log(year+'-'+(month < 10 ? '0' + month : month)+'-'+day)
    // 获取当月账单
    mAPIUrl.list('/api/mgr/guest',{
      action: 'listDate',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_rent:year+'-'+(month < 10 ? '0' + month : month)
    },function(res){
      console.log(res)
      var bill = []
      for(var i =0;i<res.guestslist.length;i++){
        var obj = {
          rent:res.guestslist[i].rent,
          tenants:res.userlist[i].name,
          roomNumber:res.roomlist[i].Room_number,
          assertTrue:res.guestslist[i].end_rent,
          show:false,
          remindRent:0,
        }
        bill.push(obj)
      }
      that.setData({
        bill:bill,
        total:res.guestslist.length,
        guestslist:res.guestslist,
        userlist:res.userlist
      })
    })
    // 获取地址信息
    mAPIUrl.list('/api/mgr/address',{
      action: 'list',
      user_id: wx.getStorageSync('userInfo').id
    },function(res){
      console.log(res)
      var array = []
      var arrayIndex=[]
      for(var i = 0;i<res.length;i++){
        array=array.concat(res[i].community+res[i].number)
        arrayIndex=arrayIndex.concat(res[i].id)
      }
      that.setData({
        array:array,
        arrayIndex:arrayIndex
      })
    })
  },

  doChange(e){
    console.log(e),
    mAPIUrl.list('/api/mgr/guest',{
      action: 'listDate',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_rent:e.detail.date
    },function(res){
      console.log(res)
      var bill = []
      for(var i =0;i<res.guestslist.length;i++){
        var obj = {
          rent:res.guestslist[i].rent,
          tenants:res.userlist[i].name,
          roomNumber:res.roomlist[i].Room_number,
          assertTrue:res.guestslist[i].end_rent,
          show:false,
          remindRent:0,
        }
        bill.push(obj)
      }
      that.setData({
        bill:bill,
        total:res.guestslist.length,
        guestslist:res.guestslist,
        userlist:res.userlist
      })
    })
  },
  pickerChange(e){
    var arrayIndex = that.data.arrayIndex
    console.log(arrayIndex[e.detail.value])
    that.setData({
      index:e.detail.value,
      address_id:arrayIndex[e.detail.value]
     })
  },
  // 办理退房
  delBtn(e){
    var id = that.data.guestslist[e.currentTarget.dataset.id].id
    mAPIUrl.update('/api/mgr/guest',{
      action: 'modify',
      id:id,
      newdata:{
        is_delete:1,
      }
    },function(res){
      console.log('id',res)
      mAPIUrl.update('/api/mgr/rsAddress',{
        action: 'modify',
        id:res.room_id,
        newdata:{
          type:0,
        }
      },function(res){
        console.log('id',res)
        wx.showToast({
          title: '办理成功',
        })
      })
    })
    onShow()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that = this
    var year=new Date().getFullYear()
    var month=new Date().getMonth()+1
    var day = new Date().getDate()
    console.log(year+'-'+(month < 10 ? '0' + month : month)+'-'+day)
    // 获取当月账单
    mAPIUrl.list('/api/mgr/guest',{
      action: 'listDate',
      landlord_id: wx.getStorageSync('userInfo').id,
      end_rent:year+'-'+(month < 10 ? '0' + month : month)
    },function(res){
      console.log(res)
      var bill = []
      for(var i =0;i<res.guestslist.length;i++){
        var obj = {
          rent:res.guestslist[i].rent,
          tenants:res.userlist[i].name,
          roomNumber:res.roomlist[i].Room_number,
          assertTrue:res.guestslist[i].end_rent,
          show:false,
          remindRent:0,
        }
        bill.push(obj)
      }
      that.setData({
        bill:bill,
        total:res.guestslist.length,
        guestslist:res.guestslist,
        userlist:res.userlist
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})